#include "VStdAfx.h"
#include "KScene.h"
#include "KLight.h"
#include "KMTRender.h"

void KScene::AddLight(VLight* Light)
{
	//KLight* Light = (KLight*)_Light;
	VASSERT(Light->m_LightAgent == NULL);

	KLightAgent* LightProxy = Light->CreateAgent();
	VASSERT(LightProxy);
	Light->m_LightAgent = LightProxy;
	LightProxy->m_Scene = this;

	m_Lights.PushBack(Light);

	ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
		RTAddLight,
		KLightAgent*,m_LightAgent,LightProxy,
	{
		m_LightAgent->RT_OnAddToScene();
	});
}

void KScene::RemoveLight(VLight* Light)
{
	VASSERT(Light);

	if (Light->m_LightAgent)
	{
		VASSERT(Light->m_LightAgent->m_Scene == this);
	
		ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
			RTRemoveLight,
			KLightAgent*,m_LightAgent,Light->m_LightAgent,
		{
			m_LightAgent->RT_OnRemoveFromScene();
		});

		Light->m_LightAgent->BeginRelease();
		Light->m_LightAgent = NULL;

		m_Lights.Remove(Light);
	}
}

void KScene::RT_GetIntersectionLights(VPODArray<KLightAgent*>& Lights)
{
	for (UINT Index = 0; Index < m_Lights.Size(); ++ Index)
	{
		KLightAgent* LightProxy = m_Lights[Index]->m_LightAgent;
		if (!LightProxy->m_Enable)
		{
			//	continue;
		}
		
		Lights.PushBack(LightProxy);


		// Test.
		LightProxy->ClearShadowCasters();

	}
}

void KScene::RT_FillShadowCasterList(KLightAgent* LightProxy)
{
	/*VPODArray<VSceneEntityAgent*>& CasterList = LightProxy->m_CasterList;
	for (UINT Index = 0; Index < m_SceneComponents.Size(); Index++)
	{

	}*/
}